package com.socialrss.utils;

import java.util.concurrent.ExecutionException;

import com.socialrss.R;
import com.socialrss.constants.SocialNetworkNames;
import com.socialrss.instagramkit.NotificationCheck;
import com.socialrss.utils.twitter.Twitts;
import com.socialrss.utils.facebook.FacebookFriendsThread;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class CheckNotificationService extends Service {

	private static final int MY_NOTIFICATION_ID = 1;

	Handler handler;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);

		Log.d("Service", "FirstService started");
		//check for internet connection
		if (Util.isConnectingToInternet(getApplicationContext())) {
			Thread instagramThread = null, twitterThread = null, facebookThread = null, googleThread = null;
			
			//check for social network connected
			if( Util.isConnected(SocialNetworkNames.INSTAGRAM, getApplicationContext()) ) {
				NotificationCheck myth = new NotificationCheck();
		        myth.execute(getApplicationContext());
		        try {
					myth.get();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ExecutionException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			if( Util.isConnected(SocialNetworkNames.TWITTER, getApplicationContext())) {
				twitterThread = new Twitts( getApplicationContext());
				twitterThread.run();
			}
			if( Util.isConnected(SocialNetworkNames.FACEBOOK, getApplicationContext())) {
				facebookThread = new FacebookFriendsThread( getApplicationContext());
				facebookThread.run();
			}
			if( Util.isConnected(SocialNetworkNames.GOOGLE, getApplicationContext())) {
				
			}
			NotificationScreenUpdateThread resultThread = new NotificationScreenUpdateThread();
			resultThread.run();
			try {
				if(twitterThread != null) {
					twitterThread.join();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			//wait for facebook thread
			try {
				if(facebookThread != null) {
					facebookThread.join();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			//wait for result thread
			try {
				resultThread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			
			
			
			Log.d("CheckNotificationService", "Finished service");
			handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					// TODO Auto-generated method stub
					super.handleMessage(msg);
					Log.d("CheckNotificationService", "Started service");
					Toast.makeText(CheckNotificationService.this,
							"1 min has passed", Toast.LENGTH_SHORT).show();

				}

			};
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d("Service", "FirstService destroyed");
	}

	private class NotificationScreenUpdateThread extends Thread {
		@Override
		public void run() {
			Log.d("CheckNotificationService", "Started waiting thread");
			long cnt = SocialRssModel.Instance(getApplicationContext())
					.getNotificationCount();
			// TODO Auto-generated method stub
			NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			Notification myNotification = new Notification(
					R.drawable.ic_launcher, "Notification!",
					System.currentTimeMillis());
			Context context = getApplicationContext();
			String notificationTitle = "Social RSS";
			String notificationText = "You have " + cnt + " notifications.";

			Intent myIntent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://android-er.blogspot.com/"));
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
					myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
			myNotification.defaults |= Notification.DEFAULT_SOUND;
			myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
			myNotification.setLatestEventInfo(context, notificationTitle,
					notificationText, pendingIntent);
			notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

		}
	}

}
